package com.angel.provider.sys.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.angel.core.base.BaseProviderImpl;
import com.angel.core.util.StringUtils;
import com.angel.dao.health.SysRoleMapper;
import com.angel.dao.health.SysRoleMenuMapper;
import com.angel.health.SysMenu;
import com.angel.health.SysRole;
import com.angel.health.SysRoleExt;
import com.angel.health.SysRoleMenu;
import com.angel.health.SysRoleMenuExample;
import com.angel.health.SysRoleMenuExample.Criteria;
import com.angel.provider.sys.SysManagerRoleProvider;

/**
 * 高亚林
 * 2016年9月20日下午5:28:31
 */
@Service
public class SysManagerRoleProviderImpl extends BaseProviderImpl<SysRole>
		implements SysManagerRoleProvider {

	@Autowired
	private SysRoleMapper mapper;
	@Autowired
	private SysRoleMenuMapper menuMapper;
	
	//查询所有角色
	public List<SysRole> findAllRole() {
		return mapper.findAllRole();
	}

	//批量删除
	public int delete(String ids) {
		try {
			String[] idarr = ids.split("&");
			for (String id : idarr) {
				SysRole role = new SysRole();
				role.setId(Integer.parseInt(id));
				role.setDelFlag("0");
				mapper.updateByPrimaryKeySelective(role);
			}
		} catch (NumberFormatException e) {
			e.printStackTrace();
			return 0;
		}
		return 1;
	}

	//删除单个用户
	public int deleteById(SysRole role) {
		role.setDelFlag("0");
		int i = mapper.updateByPrimaryKeySelective(role);
		return i;
	}

	//保存
	public Map<String, String> save(SysRole role) {
		Map<String, String> map = new HashMap<String, String>();
		if(role == null || role.getName() == null || role.getName() == ""){
			map.put("msg", "角色名称不能为空");
			map.put("code", "0");
			return map;
		}
		SysRole sysRole = new SysRole();
		sysRole.setName(role.getName());
		List<Map<String, Object>> list = mapper.findAll(sysRole);
		if(list != null && list.size() > 0){
			map.put("msg", "角色已存在");
			map.put("code", "0");
			return map;
		}
		role.setCreateTime(new Date());
		role.setDelFlag("1");
		mapper.insert(role);
		if(!StringUtils.isEmpty(role.getMenu())){
			String[] menus = role.getMenu().split(",");
			for (String menuId : menus) {
				SysRoleMenu roleMenu = new SysRoleMenu();
				roleMenu.setMenuId(Integer.parseInt(menuId));
				roleMenu.setRoleId(role.getId());
				menuMapper.insert(roleMenu);
			}
		}
		map.put("msg", "添加成功");
		map.put("code", "1");
		return map;
	}

	//根据id查询
	public SysRoleExt findUserById(SysRole role) {
		List<SysRoleExt> list = mapper.findUserById(role);
		if(list != null && list.size() > 0){
			return list.get(0);
		}
		return null;
	}

	//修改
	public Map<String, String> update(SysRole role,String menu) {
		Map<String, String> map = new HashMap<String, String>();
		if(role == null || role.getName() == null || role.getName() == ""){
			map.put("msg", "角色名称不能为空");
			map.put("code", "0");
			return map;
		}
		role.setUpdateTime(new Date());
		mapper.updateByPrimaryKeySelective(role);
		SysRoleMenuExample example = new SysRoleMenuExample();
		Criteria criteria = example.createCriteria();
		criteria.andRoleIdEqualTo(role.getId());
		menuMapper.deleteByExample(example);
		if(!StringUtils.isEmpty(menu)){
			String[] menus = menu.split(",");
			for (String menuId : menus) {
				SysRoleMenu roleMenu = new SysRoleMenu();
				roleMenu.setMenuId(Integer.parseInt(menuId));
				roleMenu.setRoleId(role.getId());
				menuMapper.insert(roleMenu);
			}
		}
		map.put("msg", "修改成功");
		map.put("code", "1");
		return map;
	}

	//根据用户查询角色
	public List<SysRole> getRolesByUser(Integer userId){
		List<SysRole> list = mapper.findRolesByUser(userId);
		return list;
	}

	//查询角色下所有用户
	public List<Map<String, Object>> showRoleUsers(Integer id) {
		return mapper.showRoleUsers(id);
	}

	//查询角色对应菜单
	public List<SysMenu> findMenuByRole(Integer id) {
		return mapper.findMenuByRole(id);
	}
}
